using System;
using System.Collections.Generic;
using System.Text;
using System.Workflow.Runtime;
using System.Workflow.Runtime.Hosting;

namespace SampleWorkflows
{

    [Serializable]
	public class PublishVideoService:IPublishVideoService  
	{

        #region IPublishVideoService Members

        /// <summary>
        /// Occurs when [video published].
        /// </summary>
        public event EventHandler<PublishEventArgs> VideoPublished;

        #endregion

        private static PublishVideoService m_instance;

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>The instance.</value>
        public static PublishVideoService Instance
        {
            get
            {
                if (m_instance == null)
                {
                    m_instance = new PublishVideoService();
                }
                return m_instance;
            }
        }

        /// <summary>
        /// Publishes the video.
        /// </summary>
        /// <param name="videoPath">The video path.</param>
        public void PublishVideo(string videoPath, string accountID, string accountKey, string filesetName)
        {
            Guid instanceId = Guid.NewGuid();

            WorkflowInstance wInstance = WorkflowsManager.Instance.Runtime.CreateWorkflow(typeof( PublishVideo),null, instanceId);
            wInstance.Start();

            PublishEventArgs e = new PublishEventArgs(instanceId);
            e.VideoPath = videoPath;
            e.FilesetName = filesetName;
            e.AccountID = accountID;
            e.AccountKey = accountKey;

            //Raise the VideoPublished Event
            PublishVideoService.Instance.VideoPublished(this, e);

            ManualWorkflowSchedulerService scheduler = WorkflowsManager.Instance.Runtime.GetService<ManualWorkflowSchedulerService>();
            scheduler.RunWorkflow(instanceId);

        }
    }
}
